package com.celesea.configuration.web;


import com.alibaba.fastjson.JSON;
import com.celesea.framework.jwt.SsoProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;

/**
 * @author JackYuan
 */
@Controller
public class WebViewController {
    private static Logger logger = LoggerFactory.getLogger(WebViewController.class);

    @Autowired
    private SsoProperties ssoProperties;


    @RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET)
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("cas登陆成功，跳转到商品BUI首页，参数：{}", JSON.toJSONString(request.getParameterMap()));
        response.sendRedirect(ssoProperties.getFe());
    }

    @ApiWrap
    @ResponseBody
    @GetMapping(path = "/current")
    public Principal getCurrentAccount(Principal principal) {
        return principal;
    }

    @ResponseBody
    @GetMapping(path = "/user")
    @PreAuthorize("hasAuthority('ROLE_USER')")
    public String user() {
        return "user";
    }

    @ResponseBody
    @GetMapping(path = "/admin")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public String admin() {
        return "admin";
    }

    @ResponseBody
    @GetMapping(path = "/test")
    @PreAuthorize("hasAuthority('test')")
    public String test() {
        return "test";
    }
}
